import type { Query } from "@/interface/Query"
import { ref   , onMounted } from "vue";
import type { TableColumnsType } from "ant-design-vue";
import type { Notice } from "@/interface/Notice";
import { useLoginStore } from "@/stores/loginStore"
import {operatorApi, listApi, changeStatusApi , removeApi } from "@/controller/NoticeApi"
import { message } from "ant-design-vue";

export default function () {
    let loginStore = useLoginStore();
    let queryModel = ref( {
        page : 1,
        size: 20,
        keyWord : '',
    } as Query);

    let searchBox = ref<boolean>(true);

    onMounted(() => {
        list();
    });

    function resetQuery() {
        queryModel.value = {
            page : 1,
            size: 20,
            keyWord : '',
        }
        list();
    }

    function list() {
        listApi(queryModel.value).then(res => {
            if (res.code == 200) {
                data.value = res.data.records;
                total.value = parseInt(res.data.totalRow);
            }
        });
    }


    let data = ref<Notice[]>([]);
    let total = ref<number>(0);
    let columns = ref<TableColumnsType>([
        {
            title: '标题',
            dataIndex: 'title',
            width: 100,
            resizable: true,
            fixed: 'left',
            align: 'center'
        },
        {
            title: '发布时间',
            dataIndex: 'publishDate',
            width: 100,
            resizable: true,
            align: 'center'
        },
        {
            title: '发布人',
            dataIndex: 'pushName',
            width: 100,
            resizable: true,
            align: 'center'
        },
        {
            title: '状态',
            dataIndex: 'status',
            width: 100,
            resizable: true,
            align: 'center'
        },
        {
            title: '类型',
            dataIndex: 'type',
            width: 100,
            align: 'center',
            resizable: true,
        },
        {
            title: '创建时间',
            dataIndex: 'createTime',
            width: 100,
            resizable: true,
            align: 'center'
        },
        {
            title: '操作',
            dataIndex: 'action',
            width: 180,
            align: 'center',
            fixed: 'right',
            resizable: true,
       }
    ]);
    function handleResizeColumn(w : any, col : any) {
        col.width = w;
    }

    let infoStore = ref({
        open : false,
        notice: {} as Notice
    });
    function info(data : Notice) {
        infoStore.value.open = true;
        infoStore.value.notice = data;
    }

    let rowsData  = ref<Notice[]>([])
    function selectData(selectedRowKeys : any, selectedRows : Notice[]) {
        rowsData.value = selectedRows;
    }

    function remove(ids ? : string[]) {
        if (ids && ids.length > 0 ) {
            removeApi(ids).then(res => {
                if (res.code == 200) {
                    message.success("删除成功！");
                    list();
                } else {
                    message.error(res.msg);
                }
            });
        }else {
            if (rowsData.value.length == 0) {return;}
            remove(rowsData.value.map(item => item.id));
        }
    }

    let operateStore = ref({
        cmd: 0,
        open : false,
        notice: {} as Notice
    });

    function operate(cmd : number, notice ? : Notice) {
        operateStore.value.cmd = cmd;
        operateStore.value.open = true;
        if (notice) {
            operateStore.value.notice = notice;
        }
    }

    function submit(data : Notice) {
        data.pushId = loginStore.loginInfo.user.user.id;
        operatorApi(data).then(res => {
            if (res.code == 200) {
                message.success(res.msg);
                list();
                resetFrom(true);
            } else {
                message.error(res.msg);
            }
        });
    }



   function resetFrom(status : boolean) {
        operateStore.value.open = false;
    }

    function pageChange(page : number, size : number) {
        queryModel.value.page = page;
        queryModel.value.size = size;
        list();
    }

    let statusStore = ref({
        open: false,
        status: 0,
        id: ''
    });
    function changeStatus(id :string, status : number) {
        statusStore.value.open = true;
        statusStore.value.id = id;
        statusStore.value.status = status;
    }

    function noStatus() {
        statusStore.value = {
            open: false,
            status: 0,
            id: ''
        }
    }

   function statusOk() {
       changeStatusApi(statusStore.value.id,statusStore.value.status).then(res => {
           if (res.code == 200) {
               list();
               noStatus();
               message.success('状态改变成功!');
           } else {
               message.error(res.msg);
           }
       })
    }

    return {
        queryModel , list , resetQuery , searchBox ,
        columns , data , handleResizeColumn ,
        selectData , operateStore , operate ,
        resetFrom , submit , total , pageChange ,
        rowsData , statusStore , changeStatus ,
        noStatus , statusOk , remove , infoStore , info
    }
}
